//
//  CorgiComposeBottomView.swift
//  weibo
//
//  Created by Corgi on 17/3/28.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

enum CorgiComposeBottomViewType: Int{
    // 图片
    case picture = 1000
    // @
    case mention = 1001
    // #
    case trend = 1002
    // 表情
    case emoticon = 1003
    // +
    case add = 1004
}

class CorgiComposeBottomView: UIView {

    var closure:((CorgiComposeBottomViewType) -> ())?
    
    var isEmoticon: Bool = false {
    
        didSet {
        
            var imgName = ""
            
            if isEmoticon {
            
                imgName = "compose_keyboardbutton_background"
                
            } else {
            
                imgName = "compose_emoticonbutton_background"
            }
            
            emoticonButton?.setImage(UIImage(named: imgName), for: .normal)
            
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), for: .highlighted)
        }
    }
    
    var emoticonButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    fileprivate func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        let pictureButton = addChildButtons(imgName: "compose_toolbar_picture", type: .picture)
        let mentionButton = addChildButtons(imgName: "compose_mentionbutton_background", type: .mention)
        let trendButton = addChildButtons(imgName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButtons(imgName: "compose_emoticonbutton_background", type: .emoticon)
        let addButton = addChildButtons(imgName: "compose_add_background", type: .add)
        
        pictureButton.snp.makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureButton.snp.right)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionButton.snp.right)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendButton.snp.right)
            make.width.equalTo(addButton)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(emoticonButton!.snp.right)
        }
    }
    
}

extension CorgiComposeBottomView {

    fileprivate func addChildButtons(imgName: String, type: CorgiComposeBottomViewType) -> UIButton {
        
        let btn = UIButton()
        
        btn.tag = type.rawValue
        
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        
        btn.setImage(UIImage(named:imgName), for: .normal)
        
        btn.setImage(UIImage(named:"\(imgName)_highlighted"), for: .highlighted)
        
        addSubview(btn)
        
        return btn
    }
    
    @objc fileprivate func btnClick(button: UIButton) {
        
        closure?(CorgiComposeBottomViewType(rawValue: button.tag)!)
    }

}
